package com.example.controller;

import com.example.pojo.DepartItem_Pojo;
import com.example.service.DepartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/departItem")
public class DepartItemController {

    @Autowired
    private DepartItemService departItemService;

    /**
     * 查询所有部门信息
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<DepartItem_Pojo> departItems = departItemService.getDepartItem();
        model.addAttribute("departItems", departItems);
        return "departItem/list";
    }

    /**
     * 根据ID查询部门名称
     */
    @GetMapping("/get/{id}")
    public String getDepartName(@PathVariable String id, Model model) {
        String departName = departItemService.getDepartNameByID(id);
        model.addAttribute("departName", departName);
        model.addAttribute("departID", id);
        return "departItem/detail";
    }

    /**
     * 显示添加部门表单页面
     */
    @GetMapping("/addForm")
    public String showAddForm(Model model) {
        model.addAttribute("departItem", new DepartItem_Pojo());
        return "departItem/addForm";
    }

    /**
     * 添加部门信息
     */
    @PostMapping("/add")
    public String addDepartItem(@ModelAttribute DepartItem_Pojo departItem) {
        departItemService.insertDepartItem(departItem);
        return "redirect:/departItem/list";
    }

    /**
     * 显示编辑部门表单页面
     */
    @GetMapping("/editForm")
    public String showEditForm(@RequestParam String id, Model model) {
        String departName = departItemService.getDepartNameByID(id);
        DepartItem_Pojo departItem = new DepartItem_Pojo();
        departItem.setDepartID(id);
        departItem.setDepartName(departName);
        model.addAttribute("departItem", departItem);
        return "departItem/editForm";
    }

    /**
     * 更新部门信息
     */
    @PostMapping("/update")
    public String updateDepartItem(@ModelAttribute DepartItem_Pojo departItem) {
        departItemService.updateDepartItem(departItem);
        return "redirect:/departItem/list";
    }

    /**
     * 删除部门信息
     */
    @PostMapping("/delete/{id}")
    public String deleteDepartItem(@PathVariable String id) {
        departItemService.deleteDepartItem(id);
        return "redirect:/departItem/list";
    }
}